package cm.cool.web.service.notice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import cm.cool.web.bean.Notice;
import cm.cool.web.data.NoticeMapper;
import cm.cool.web.util.respon.PaginationDto;
import cm.cool.web.util.respon.R;
import cm.cool.web.util.respon.ResultEncoding;

/**
 * 警告信息
 * @author fangzhenxing
 *
 */
@Service
public class NoticeService {
	
	@Autowired
	NoticeMapper noticeMapper;
	
	/**
	 * 查询所有的警告信息
	 */
	public R selectNoticeAll(Notice notice,PaginationDto paginationDto){
		R r=new R();
		
		try {
			PageHelper.startPage(paginationDto.getCurrentPageNum(), paginationDto.getPerPageSize());
			paginationDto.setEntitys(noticeMapper.selectByPrimaryKey(notice));
			int count = noticeMapper.selectByAllCount(notice);
			paginationDto.setTotalCount(count);
			paginationDto.setTotalPageNum((int)Math.ceil((count*1.0) / paginationDto.getPerPageSize()));
			r.setData(paginationDto);
		} catch (Exception e) {
			e.printStackTrace();
			r.setCode(ResultEncoding.R_ERR);
			r.setMsg("查询异常");
			return r;
		}
		
		return r;
	}
	
	/**
	 * 添加一个警告信息
	 * @return
	 */
	public R insertNotice(Notice notice){
		R r=new R();
		
		try {
			noticeMapper.insert(notice);
		} catch (Exception e) {
			e.printStackTrace();
			r.setCode(ResultEncoding.R_ERR);
		}
		return r;
	}
}
